<script setup lang="ts">
import Sidebar from "./components/Sidebar.vue";
import ChatInput from "./components/ChatInput.vue";
import ChatMessages from "./components/ChatMessages.vue";
import SystemPrompt from "./components/SystemPrompt.vue";
import ModelSelector from "./components/ModelSelector.vue";
import {
  currentModel,
  isDarkMode,
  isSettingsOpen,
  isSystemPromptOpen
} from "@/views/domain/knowledge/config/ollama";
import { onMounted, ref } from "vue";
import { useAI } from "@/views/domain/knowledge/store/useAI";
import { useChats } from "@/views/domain/knowledge/store/chat";
import TextInput from "./components/Inputs/TextInput.vue";
import Settings from "./components/Settings.vue";

const { refreshModels, availableModels } = useAI();
const { activeChat, renameChat, switchModel, initialize, hasMessages } =
  useChats();
const isEditingChatName = ref(false);
const editedChatName = ref("");

const startEditing = () => {
  isEditingChatName.value = true;
  editedChatName.value = activeChat.value?.name || "";
};

const cancelEditing = () => {
  isEditingChatName.value = false;
  editedChatName.value = "";
};

const confirmRename = () => {
  if (activeChat.value && editedChatName.value) {
    renameChat(editedChatName.value);
    isEditingChatName.value = false;
  }
};

onMounted(() => {
  refreshModels().then(async () => {
    await initialize();
    await switchModel(currentModel.value ?? availableModels.value[0].name);
  });
});
</script>

<template>
  <el-container class="dialog-body">
    <el-aside width="12rem">
      <Sidebar />
    </el-aside>

    <el-container>
      <el-header>
        <div style="display: flex; justify-content: space-between; flex: 1">
          <div v-if="activeChat" class="mr-auto flex h-full items-center">
            <div>
              <div v-if="isEditingChatName">
                <TextInput
                  v-model="editedChatName"
                  autofocus
                  @keyup.enter="confirmRename"
                  @keyup.esc="cancelEditing"
                  @blur="cancelEditing"
                />
              </div>

              <el-button v-else type="text" @click="startEditing">
                {{ activeChat.name }}
              </el-button>
            </div>
          </div>

          <ModelSelector :disabled="hasMessages" />
        </div>
      </el-header>
      <el-main>
        <div class="chat-wrapper">
          <ChatMessages />
        </div>
        <ChatInput />
      </el-main>
    </el-container>
    <el-aside v-if="isSettingsOpen">
      <Settings />
    </el-aside>
  </el-container>
</template>

<style lang="scss" scoped>
.dialog-body {
  // height: 44.5rem !important;
  height: calc(100vh - var(--lay-footer-height) - 81px) !important;

  .el-container {
    background-color: var(--el-bg-color);
    .el-header {
      border-bottom: 1px solid var(--el-border-color);
      display: flex;
      align-items: center;
    }

    .el-main {
      padding: 8px !important;
      display: flex;
      flex-direction: column;

      .chat-wrapper {
        flex: 1;
        overflow-y: auto;
        max-height: calc(100% - var(--el-header-height) - 2px);
      }
    }
  }
}
</style>
